﻿using GameCreator.Core;
using Gp.GameLogic;
using Gp.Scripts.Data;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.Core {
    public class LootInventory : BaseInventory {
        [SerializeField] private InteractableItem interactableItem;
        [SerializeField] private UnitInventory dataObjects;
        [SerializeField] public string displayName;

        public string DisplayName => (displayName.IsNullOrEmpty() ? "箱子" : displayName);

        private GlobalID _id;
        public override string SaveKey {
            get {
                // if (_id == null) {
                //     _id = GetComponent<GlobalID>();
                // }

                return _id == null ? "null" : _id.GetID();
            }
        }


        public InteractableItem InteractableItem => interactableItem;
        public Vector3 PivotPos => interactableItem ? interactableItem.DrawerSource.position : Vector3.zero;
        public Transform PivotTransform => interactableItem.DrawerSource;


        public bool IsOpening { get; set; }


        private void Start() {
            // if (interactableItem != null) {
            //     interactableItem.AddInfoEntry(
            //         new InteractEntryInfo(
            //             () => "打开：" + DisplayName,
            //             OpenInventory));
            // }
            //
            // var model = MainArchitecture.Interface.GetModel<InventoryModel>();
            // var data = model.GetData(this);
            // if (data != null) {
            //     FillData(data);
            //     return;
            // }

            // Content =
            //     dataObjects.slots.Where(x => x.item != null).Select(x => {
            //         var obj = x.item.Build() as InventoryItem;
            //         obj.Quantity = x.itemCount;
            //         return obj;
            //     }).ToArray();
        }


        public void Fill(UnitInventory other) {
            // this.dataObjects.CopyFrom(other);
            // Content =
            //     dataObjects.slots.Where(x => x.item != null).Select(x => {
            //         var obj = x.item.Build() as InventoryItem;
            //         obj.Quantity = x.itemCount;
            //         return obj;
            //     }).ToArray();
        }

        
        public void Fill(UnitInventory other, UnitEquip equip) {
            // dataObjects.slots.Clear();
            // foreach (var eq in equip.weaponEquips) {
            //     if (eq == null) continue;
            //     dataObjects.Add(eq);
            // }
            //
            // var armor = equip.ArmorEquip;
            // armor.isBroken = true;
            // dataObjects.Add(armor);
            // dataObjects.slots.AddRange(other.slots);
            //
            //
            // Content =
            //     dataObjects.slots.Where(x => x.item != null).Select(x => {
            //         var obj = x.item.Build() as InventoryItem;
            //         obj.Quantity = x.itemCount;
            //         return obj;
            //     }).ToArray();
        }


        private void OpenInventory() {
            // if (Content.Count(x => x != null) == 0) return;
            Global.Get<InventorySystem>().TryOpenLootUI(this);
        }


        protected override void OnContentChange() {
            // if (Content.Count(x => x != null) == 0 && interactableItem) {
            //     interactableItem.IsActivate = false;
            //     interactableItem.IsVisible = false;
            // }
        }
    }
}